package com.prs.crm.action.oa.report;

import java.util.Collection;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.oa.JobPlan;
import com.prs.crm.report.ChartResult;
import com.prs.crm.report.ChartService;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.oa.JobPlanService;

public class DeptJobPlanReportAction extends ReportAction {

	private static final long serialVersionUID = 3170739380374208843L;
	private JobPlanService jobPlanService;
	private ChartResult chartResult;
	private ChartService chartService;
	private PaginationSupport pagination;
	private String category;
	private int exportType;

	private Date datestart;
	private Date dateend;

	@Action("editDeptJobPlanReport")
	public String editDeptJobPlanReport() {
		return SUCCESS;
	}

	@Action(value = "viewDeptJobPlanPie", results = {
			@Result(name = "success", type = "chain", location = "editDeptJobPlanReport"),
			@Result(name = "error", type = "chain", location = "editDeptJobPlanReport") })
	public String viewDeptJobPlanPie() {
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		Collection<?> dataList = this.getJobPlanService()
				.getDeptReportJobPlanSource(datestart, dateend);
		chartResult = this.getChartService().export(
				this.getJobPlanService().fillDataset(dataList,
						Constrains.REPORT_PIECHART), "部门工作计划分析",
				"listReportDeptJobPlan");
		return SUCCESS;
	}

	@Action(value = "viewDeptJobPlanBar", results = {
			@Result(name = "error", type = "chain", location = "editDeptJobPlanReport"),
			@Result(name = "success", type = "chain", location = "editDeptJobPlanReport") })
	public String viewDeptJobPlanBar() {
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		Collection<?> dataList = this.getJobPlanService()
				.getDeptReportJobPlanSource(datestart, dateend);
		chartResult = this.getChartService().export(
				this.getJobPlanService().fillDataset(dataList,
						Constrains.REPORT_BARCHART), "部门工作计划分析", "数量", "",
				"listReportDeptJobPlan");
		return SUCCESS;
	}

	@Action("listReportDeptJobPlan")
	public String listReportDeptJobPlan() {
		category = ReportUtils.URLDecode(this.getCategory());
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				JobPlan.class);
		if (datestart != null)
			criteria.add(Restrictions.ge("recordTime", datestart));
		if (dateend != null)
			criteria.add(Restrictions.le("recordTime", dateend));
		if (category != null && category.trim().length() > 0) {
			criteria.createAlias("recorder", "re");
			criteria.createAlias("re.dept", "de");
			criteria.add(Restrictions.eq("de.name", category));
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getJobPlanService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "exportDeptJobPlanReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String exportDeptJobPlanReport() {
		ReportTable table = new ReportTable("部门工作计划报表");
		table.addCell(new ReportCell("序号", "id"));
		table.addCell(new ReportCell("主题", "title"));
		table.addCell(new ReportCell("开始时间", "startTime"));
		table.addCell(new ReportCell("结束时间", "endTime"));
		table.addCell(new ReportCell("完成量(%)", "percent"));
		table.addCell(new ReportCell("主送", "auditPerson.resume.name"));
		table.addCell(new ReportCell("提交人", "recorder.resume.name"));
		table.addCell(new ReportCell("部门", "recorder.dept.name"));
		table.addCell(new ReportCell("类型", "type"));
		table.addCell(new ReportCell("状态", "state", Constrains
				.getReportJobPlan()));
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				JobPlan.class);
		if (datestart != null)
			criteria.add(Restrictions.ge("recordTime", datestart));
		if (dateend != null)
			criteria.add(Restrictions.le("recordTime", dateend));
		if (category != null && category.trim().length() > 0) {
			criteria.createAlias("recorder", "re");
			criteria.createAlias("re.dept", "de");
			criteria.add(Restrictions.eq("de.name", category));
		}
		this.setOutFilePath(this.getReportService().report(criteria, table,
				new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}

	public JobPlanService getJobPlanService() {
		return jobPlanService;
	}

	public void setJobPlanService(JobPlanService jobPlanService) {
		this.jobPlanService = jobPlanService;
	}

	public ChartResult getChartResult() {
		return chartResult;
	}

	public void setChartResult(ChartResult chartResult) {
		this.chartResult = chartResult;
	}

	public ChartService getChartService() {
		return chartService;
	}

	public void setChartService(ChartService chartService) {
		this.chartService = chartService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public int getExportType() {
		return exportType;
	}

	public void setExportType(int exportType) {
		this.exportType = exportType;
	}

	public Date getDatestart() {
		return datestart;
	}

	public void setDatestart(Date datestart) {
		this.datestart = datestart;
	}

	public Date getDateend() {
		return dateend;
	}

	public void setDateend(Date dateend) {
		this.dateend = dateend;
	}

}
